package com.yaoandw.crawler;

import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.yaoandw.util.StringUtil;

public class UrlTool {
	public static String processUrlHash(String surl) {
		if(!StringUtil.isEmpty(surl)){
			if(surl.indexOf("#!") != -1)
				surl.replace("#!", "?_escaped_fragment_=");//same as google http://www.ruanyifeng.com/blog/2011/03/url_hash.html
			if(surl.indexOf("#") != -1)
				surl = surl.substring(0, surl.indexOf("#"));
		}
		return surl;
	}
	public static String processUrl(String surl) {
		if(isChinese(surl)){
			try{
				String fileName = surl.substring(surl.lastIndexOf("/")+1);
				surl = surl.substring(0,surl.lastIndexOf("/")+1)+URLEncoder.encode(fileName, "utf-8");
				return surl;
			}catch(Exception e){
				e.printStackTrace();
			}
		}else
			return surl;
		return null;
	}
	private static boolean isChinese(String s){
		s=new String(s.getBytes());//用GBK编码
        String pattern="[\u4e00-\u9fa5]+";  
        Pattern p=Pattern.compile(pattern);  
        Matcher result=p.matcher(s);                  
        return result.find(); //是否含有中文字符 
	}
	public static void main(String[] args){
//		String url = "http://www.lietu.com/case/猎兔企业搜索系统解决方案.doc";
		String url = "http://www.actiz.com:8080/bbs/forum.php?mod=viewthread&tid=64&extra=page%3D1";
		System.out.println(processUrl(url));
	}
}
